package cn.tedu.project.oa.management.controller;

import cn.tedu.project.oa.base.response.JsonResult;
import cn.tedu.project.oa.base.webSocket.CustomWebSocketEndpoint;
import cn.tedu.project.oa.management.pojo.dto.ManagementGetListQuery;
import cn.tedu.project.oa.management.pojo.dto.ManagementAddParam;
import cn.tedu.project.oa.management.pojo.dto.MemberUpdateParam;
import cn.tedu.project.oa.management.pojo.vo.ManagementInfoVO;
import cn.tedu.project.oa.management.pojo.vo.ManagementVO;
import cn.tedu.project.oa.management.service.ManagementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import shade.com.alibaba.fastjson2.JSON;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/oa/attendance/management")
@Api(tags = "07.考勤管理")
public class ManagementController {
    @Autowired
    private ManagementService managementService;

    /**
     * 查询考勤管理列表
     */
    @GetMapping("/list")
    @ApiOperation("查询考勤管理列表")
    public JsonResult list(ManagementGetListQuery managementGetListQuery)
    {
        List<ManagementVO> list = managementService.selectManagementList(managementGetListQuery);
        return JsonResult.ok(list);
    }

//    /**
//     * 导出考勤管理列表
//     */
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, Management management)
//    {
//        List<Management> list = managementService.selectManagementList(management);
//        ExcelUtil<Management> util = new ExcelUtil<Management>(Management.class);
//        util.exportExcel(response, list, "考勤管理数据");
//    }

    /**
     * 获取考勤管理详细信息
     */
    @GetMapping(value = "/getInfo/{id}")
    @ApiOperation("获取考勤管理详细信息")
    public JsonResult getInfo(@PathVariable("id") Long id)
    {
        log.debug("getInfo(id={})", id);
        ManagementInfoVO managementInfoVO = managementService.selectManagementById(id);
        log.debug("managementInfoVO={}", managementInfoVO);
        return JsonResult.ok(managementInfoVO);
    }

    /**
     * 新增考勤管理
     */
    @PostMapping("/add")
    @ApiOperation("新增考勤管理")
    public JsonResult add(ManagementAddParam managementAddParam)
    {
        managementService.insertManagement(managementAddParam);
        return JsonResult.ok();
    }

    //更新参与考勤人员
    @PostMapping("/update/member")
    @ApiOperation("更新参与考勤人员")
    public JsonResult updateMember(@RequestBody MemberUpdateParam memberUpdateParam){
        managementService.updateMember(memberUpdateParam);
        return JsonResult.ok();
    }

    /**
     * 删除考勤管理
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除考勤管理")
    public JsonResult remove(@PathVariable Long id)
    {
        return JsonResult.ok(managementService.deleteManagementById(id));
    }

    //TODO被Token拦截站内信测试
    @GetMapping("addNum")
    @ResponseBody
    public String sendMessage() {
        String userId = "1";
        Map<String,String> map = new HashMap<>();
        map.put("message","新消息");
        String message = "{message : '新消息'}";

        try {
            CustomWebSocketEndpoint.sendMessageToUser(userId, JSON.toJSONString(map));
            return "发送消息成功:" + userId;
        } catch (IOException e) {
            e.printStackTrace();
            return "发送消息失败";
        }
    }
}
